/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.alarm.mapping;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Date;
import java.util.Iterator;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;

import twaver.Alarm;
import twaver.AlarmModelEvent;
import twaver.AlarmModelListener;
import twaver.AlarmSeverity;
import twaver.Element;
import twaver.ElementAttribute;
import twaver.ElementAttributeVisibleFilter;
import twaver.Node;
import twaver.SubNetwork;
import twaver.TDataBox;
import twaver.TWaverUtil;
import twaver.VisibleFilter;
import twaver.network.TNetwork;
import twaver.swing.TExpandPane;
import twaver.table.TAlarmOverview;
import twaver.table.TAlarmTable;
import twaver.table.TPropertySheet;
import twaver.table.TTableColumn;
import twaver.table.renderer.AcknowledgedRenderer;
import twaver.table.renderer.StringRenderer;
import twaver.tree.TTree;
import demo.DemoPane;

public class AlarmElementMappingDemo extends DemoPane{
	  
	private final static String GROUP_G = "Geography Group"; 
	private final static String GROUP_B = "Business Group"; 
	private final static String GROUP_M = "Manufacturer Group"; 
	
	private final static Color COLOR_G = new Color(100, 100, 240);
	private final static Color COLOR_B = Color.PINK;
	private final static Color COLOR_M = Color.GREEN;

	private int count = 0;
	private JSplitPane mainUI = new JSplitPane();
	private TDataBox box = new TDataBox("OSS");
	private TNetwork network = new TNetwork(box);
	private TTree tree = new TTree(box);
	private TAlarmOverview overview = new TAlarmOverview(box);
	private JSplitPane rigthSplit = new JSplitPane();
	private TAlarmTable alarmTable = new TAlarmTable(box, createDefaultColumns());
	private TPropertySheet sheet = new TPropertySheet(box);
	
	public AlarmElementMappingDemo() {
		
		// set custom alarm element mapping
		box.getAlarmModel().setAlarmElementMapping(new CustomAlarmElementMapping(box));
		// set statistics visible filter
		overview.setVisibleFilter(new VisibleFilter(){
			public boolean isVisible(Element element) {
				return !Boolean.FALSE.equals(element.getClientProperty("stat"));
			}
		});
		
		sheet.setMode(TPropertySheet.VIEW_AS_FLAT_LIST);
		sheet.setElementAttributeVisibleFilter(new ElementAttributeVisibleFilter(){
			public boolean isVisible(ElementAttribute attribute) {
				String name = attribute.getName();
				return "alarmState".equals(name) || "name".equals(name);
			}
		});
		
		JPanel topPanel = new JPanel(new BorderLayout());
		topPanel.add(sheet, BorderLayout.SOUTH);
		topPanel.add(new JScrollPane(tree), BorderLayout.CENTER);
		
		TExpandPane bottomPanel = new TExpandPane(overview, TExpandPane.SOUTH, true, true);
		
		JPanel leftPanel = new JPanel(new BorderLayout());
		leftPanel.add(topPanel, BorderLayout.CENTER);
		leftPanel.add(bottomPanel, BorderLayout.SOUTH);

		mainUI.setContinuousLayout(true);
		mainUI.add(rigthSplit, JSplitPane.RIGHT);
		mainUI.add(leftPanel, JSplitPane.LEFT);

		alarmTable.setEnableTristateSorting(false);
		alarmTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);

		JPanel tablePanel = new JPanel(new BorderLayout());
		tablePanel.add(new JScrollPane(alarmTable), BorderLayout.CENTER);
		
		final JButton createAlarm = new JButton("CreateAlarm");
		final JButton reverseAcked = new JButton("!Acked");
		final JButton reverseCleared = new JButton("!Cleared");
		final JButton clearAll = new JButton("ClearAll");
		final JCheckBox removeOnCleared = new JCheckBox("RemoveOnCleared", box.getAlarmModel().isAutoRemoveAlarmWhenCleared());
		
		JPanel buttonPane=new JPanel();
		buttonPane.add(reverseAcked);
		buttonPane.add(reverseCleared);
		buttonPane.add(createAlarm);
		buttonPane.add(clearAll);
		buttonPane.add(removeOnCleared);
		tablePanel.add(buttonPane,BorderLayout.NORTH);
		
		rigthSplit.setOrientation(JSplitPane.VERTICAL_SPLIT);
		rigthSplit.add(network, JSplitPane.TOP);
		rigthSplit.add(tablePanel, JSplitPane.BOTTOM);

		tree.setEnsureVisibleOnSelected(true);
		tree.setEnsureVisibleOnSelected(true);
		network.setEnableDeleteWithKeyboard(true);
		network.setEnsureVisibleOnSelected(true);
		network.setEnableDeleteWithKeyboard(true);
		
		mainUI.setDividerLocation(200);
		rigthSplit.setDividerLocation(400);
		this.setLayout(new BorderLayout());
		this.add(mainUI,BorderLayout.CENTER);
		
		createGroupContent(GROUP_G, 200, 100, COLOR_G, true);
		createGroupContent(GROUP_B, 100, 200, COLOR_B, false);
		createGroupContent(GROUP_M, 300, 200, COLOR_M, false);
		
		tree.expandAll();

		reverseAcked.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Iterator it = alarmTable.getAllSelectedAlarms().iterator();
				while (it.hasNext()) {
					Alarm alarm = (Alarm) it.next();
					alarm.setAcked(!alarm.isAcked());
				}
			}
		});
		reverseCleared.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Iterator it = alarmTable.getAllSelectedAlarms().iterator();
				while (it.hasNext()) {
					Alarm alarm = (Alarm) it.next();
					alarm.setCleared(!alarm.isCleared());
				}
			}
		});
		createAlarm.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
		        Alarm alarm = new Alarm("AlarmID_" + count++);
		        alarm.setAcked(TWaverUtil.getRandomBool());
		        alarm.setRaisedTime(new Date());
		        alarm.setAlarmSeverity(AlarmSeverity.getRandomSeverity());
		        alarm.putClientProperty("mappingID", TWaverUtil.getRandomInt(5)+1);
		        box.getAlarmModel().addAlarm(alarm);
			}
		});
		clearAll.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				box.getAlarmModel().clear();
			}
		});
		removeOnCleared.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				box.getAlarmModel().setAutoRemoveAlarmWhenCleared(removeOnCleared.isSelected());
			}
		});
		
		box.getAlarmModel().addAlarmModelListener(new AlarmModelListener(){
			public void alarmAdded(AlarmModelEvent e) {
				System.out.println(e.getAlarm().getAlarmID() + " is added");
			}
			public void alarmRemoved(AlarmModelEvent e) {
				System.out.println(e.getAlarm().getAlarmID() + " is removed");
			}
			public void alarmCleared(AlarmModelEvent e) {
				System.out.println("alarms are all cleared");
			}
		});
		box.getAlarmModel().addAlarmPropertyChangeListener(new PropertyChangeListener(){
			public void propertyChange(PropertyChangeEvent evt) {
				System.out.println(evt.getPropertyName() + ":" + evt.getNewValue());
			}
		});
	}
	
    private TTableColumn[] createDefaultColumns() {
        return new TTableColumn[] {
        		new TTableColumn(Alarm.PROPERTY_ALARMID, 80),
        		new TTableColumn("mappingID", 80).setRenderer(new StringRenderer("center", "true")),
        		new TTableColumn(Alarm.PROPERTY_ALARMSEVERITY, 100).setJavaClass(AlarmSeverity.class).setEditable(true),
        		new TTableColumn(Alarm.PROPERTY_ACKED, 80).setRenderer(new AcknowledgedRenderer()), 
        		new TTableColumn(Alarm.PROPERTY_CLEARED, 80).setRenderer(new AcknowledgedRenderer()), 
        		new TTableColumn(Alarm.PROPERTY_RAISEDTIME, 180),
        		
        };
    }
    
	private void createGroupContent(String groupName, int x, int y, Color color, boolean stat){
		SubNetwork group = new SubNetwork(groupName);
		group.setName(groupName);
		group.setLocation(x, y);
		group.setColorBackground(color);
		group.putLabelColor(color);
		box.addElement(group);
		
		for(int i=1; i<=5; i++){
			Node node = new Node();
			node.setName("BSC_" + i);
			node.setLocation(i*100, 80);
			node.setParent(group);
			
			node.putClientProperty("mappingID", i);
			node.putClientProperty("belongTo", groupName);
			node.putClientProperty("stat", stat);
			box.addElement(node);
		}
	}
	

}